package com.co.webmagic.process;

import com.co.common.util.IdWorker;
import com.co.video.pojo.Detail;
import com.co.video.pojo.Video;
import com.co.video.pojo.VideoState;
import com.co.video.service.CategoryService;
import com.co.video.service.DetailService;
import com.co.video.service.VideoService;
import com.co.video.service.VideoStateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author ldl.plus
 * @date 2020年06月30日  23:08
 */
@Component
public class NewJobProcessor implements PageProcessor {
    /**
     * 设置拼接的url变量
     */
    public static final String urlPre = "https://www.bilibili.com/";

    @Autowired
    private DetailService detailService;

    @Autowired
    private VideoService videoService;

    @Autowired
    private VideoStateService videoStateService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private IdWorker idWorker;
    private static final Logger log = LoggerFactory.getLogger(NewJobProcessor.class);

    /**
     * 解析页面
     *
     * @param page
     */
    @Override
    public void process(Page page) {
        Html html = page.getHtml();
        List<Selectable> list = html.css("div.mixin-list ul.video-list li.video-item").nodes();
        if (list != null && list.size() > 0) {
            // 有推荐列表，保存数据，并把URL加到爬取列表
            for (Selectable selectable : list) {
                String urlPost = selectable.css("a", "href").get();
                String imageSrc = selectable.css("div.lazy-img img", "src").get();
                String duration = selectable.css("span.so-imgTag_rb", "text").get();
                String title = selectable.css("a", "title").get();
                String comment = selectable.css("div.info div.des", "text").get();
                String view = selectable.css("div.tags i.watch-num", "text").get();
                String dm = selectable.css("div.tags i.hide", "text").get();
                String uploadDate = selectable.css("div.tags i.time", "text").get();
                String username = selectable.css("div.tags a.up-name", "text").get();
                page.addTargetRequest(urlPost);
                Detail detail = new Detail();
                detail.setAid(idWorker.nextId() + "");
                detail.setTitle(title);
                detail.setImage(imageSrc);
                detail.setUrl(urlPost.substring(2));
                try {
                    detail.setUploadTime(new SimpleDateFormat("yyyy-MM-dd").parse(uploadDate));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                String[] times = duration.split(":");
                detail.setDurations(Integer.parseInt(times[0]) * 60 + Integer.parseInt(times[1]));
                detail.setStatus(1);
                detail.setUsername(username);
                detail.setIsUpload(1);
                detail.setComment(comment);
                detail.setVideos(1);
                detail.setCreateTime(detail.getUploadTime());
                detail.setUpdateTime(detail.getUploadTime());

                Video video = new Video();
                video.setCid(idWorker.nextId() + "");
                video.setAid(detail.getAid());
                video.setCreateTime(detail.getUploadTime());
                video.setUpdateTime(detail.getUploadTime());
                video.setPage(1);
                video.setFrom("crawler");
                video.setPart("1");
                video.setDuration(detail.getDurations());
                video.setUrl(detail.getUrl());

                VideoState videoState = new VideoState();
                videoState.setId(idWorker.nextId() + "");
                videoState.setAid(detail.getAid());
                videoState.setView(Integer.parseInt(view));
                videoState.setDanmaku(Integer.parseInt(dm));
                videoState.setCreateTime(detail.getUploadTime());
                videoState.setUpdateTime(detail.getUploadTime());

                detailService.add(detail);
                videoService.add(video);
                videoStateService.add(videoState);
                log.info("---------" + detail.getUrl() + "------" + "在搜索页面保存部分信息");
            }
        } else {
            // 详情页，保存相关数据

        }


    }

    /**
     * 重试次数和抓取间隔
     */
    private final Site site = Site.me()
            .setCharset("UTF8")
            .setTimeOut(3000)
            .setRetrySleepTime(1000)
            .setRetryTimes(3)
            .setSleepTime(0)
            .setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36 Edg/83.0.478.56");

    @Override
    public Site getSite() {
        return site;
    }
}
